from django.http import HttpResponse
from django.views.generic.base import View

from apps.operations.modelForm import UserAskForm,UserFavorite
from apps.operations.models import CourseComments
from apps.organizations.models import Teacher
from apps.users.utils.mixin import LoginRequiredMiXin
from apps.courses.models import Org, Course


class UserAskView(View):
    """
    ajax提交咨询课程的信息
    """
    def post(self,request):
        userAskForm = UserAskForm(request.POST)
        if userAskForm.is_valid():
            userAskForm.save(commit=True)
            return HttpResponse("{\"status\":\"success\"}",content_type="application/json")
        return HttpResponse("{\"status\":\"fail\",\"msg\":\"输入内容有误!\"}",content_type="application/json")


class UserFavoriteView(LoginRequiredMiXin,View):
    """
    ajax提交用户（取消）收藏(机构:1,教师:2,课程:3)的信息
    """
    def post(self,request):
        favId = request.POST.get("favId","")
        favType = request.POST.get("favType","")
        userFavorite = UserFavorite.objects.filter(user=request.user,favId=int(favId),favType=int(favType))
        if userFavorite:# 取消收藏
            userFavorite.delete()
            if int(favType) == 1:
                org = Org.objects.get(id=int(favId))
                org.favNum -= 1
                if org.favNum < 0:
                    org.favNum = 0
                org.save()
            elif int(favType) == 2:
                teacher = Teacher.objects.get(id=int(favId))
                teacher.favNum -= 1
                if teacher.favNum < 0:
                    teacher.favNum = 0
                teacher.save()
            else:
                course = Course.objects.get(id=int(favId))
                course.favNum -= 1
                if course.favNum < 0:
                    course.favNum = 0
                course.save()
            return HttpResponse("{\"status\":\"success\",\"msg\":\"取消收藏成功\"}",content_type="application/json")
        else:# 收藏
            if int(favId) > 0 and int(favType) > 0:
                userFavorite = UserFavorite()
                userFavorite.favId = int(favId)
                userFavorite.favType = int(favType)
                userFavorite.user = request.user
                userFavorite.save()
                if int(favType) == 1:
                    org = Org.objects.get(id=int(favId))
                    org.favNum += 1
                    org.save()
                elif int(favType) == 2:
                    teacher = Teacher.objects.get(id=int(favId))
                    teacher.favNum += 1
                    teacher.save()
                else:
                    course = Course.objects.get(id=int(favId))
                    course.favNum += 1
                    course.save()
                return HttpResponse("{\"status\":\"success\",\"msg\":\"收藏成功\"}",content_type="application/json")
            else:
                return HttpResponse("{\"status\":\"fail\",\"msg\":\"收藏失败\"}",content_type="application/json")


class UserCommentView(LoginRequiredMiXin,View):
    """
    ajax提交用户评论课程的信息
    """
    def post(self,request):
        courseId = request.POST.get("courseId","")
        comments = request.POST.get("comments","")
        if courseId and comments:
            courseComment = CourseComments()
            courseComment.user = request.user
            courseComment.course_id = int(courseId)
            courseComment.comments = comments
            courseComment.save()
            return HttpResponse("{\"status\":\"success\",\"msg\":\"评论成功！\"}",content_type="application/json")
        return HttpResponse("{\"status\":\"fail\",\"msg\":\"评论失败！\"}",content_type="application/json")


